/* eslint-disable space-before-function-paren */
/*
 * @Author: Aaron
 * @Date: 2022-03-01 11:18:12
 * @LastEditors: YanMeng
 * @LastEditTime: 2022-10-18 10:51:06
 * @Description: file content
 * @FilePath: \ias_ui\src\views\coi\spuManagement\js\maintain.js
 * @Company: 沃尔玛
 */

import moment from 'moment'
// import { downloadWares } from '@/api/coi/onlineTaskAPI/spuEdit.js'
import { getSpuInfoPageList, pushSpu, spuListStatus, processData, abandonRequest } from '@/api/coi/onlineTaskAPI/spu.js'
import {
  getAllMainStatusTypes,
  procurementList,
  textStrokeList,
  getProcessLogByChgSpuId,
  abandonEditRequest,
  spuSubmit_text,
  pushSpu_text,
  withdraw_edit
} from '@/api/coi/onlineTaskAPI/spuEdit.js'
import { stringToArray } from '@/utils/processingData'
import { spuOperationLogTableHeader } from '@/views/coi/commonJs/spuWarehouseTableHead.js'
import bus from '@/utils/eventBus.js'

export const requestTableDataFun = {
  data() {
    return {
      getTableListFun: getSpuInfoPageList, // 获取列表的请求api
      flag: false
    }
  },
  created() {
    this.chooseTableFun()
    this.handleSearch()
  },
  activated() {
    this.handleSearch()
  },
  methods: {
    chooseTableFun() {
      if (this.$route.name === 'SpuTeamDescription' && this.menuName === 'newProducts') {
        // 文描新增
        this.getTableListFun = getSpuInfoPageList
      } else if (this.$route.name === 'OnlineBuildingTasks' && this.menuName === 'safeguard') {
        // 采销维护
        this.getTableListFun = procurementList
      } else if (this.$route.name === 'SpuTeamDescription' && this.menuName === 'safeguard') {
        // 文描维护
        this.getTableListFun = textStrokeList
      }
    },
    handleData() {
      const searchData = JSON.parse(JSON.stringify(this.searchData))
      if ('skuList' in searchData) searchData.skuList = stringToArray(searchData.skuList, /[\n\r]/)
      return searchData
    },
    changeLoading(v = true) {
      bus.$emit('changeLoading', v)
    },
    async requestData() {
      try {
        this.changeLoading(true)
        const { result } = await this.getTableListFun({ ...this.handleData(), ...this.pageParams })
        if (!result) return
        if (Array.isArray(result.list)) {
          result.list.forEach((e) => {
            if (e.updateTime) e.updateTime = moment(e.updateTime).format('YYYY-MM-DD HH:mm:ss')
          })
        }
        this.total = result.total || 0
        this.dataList = result.list || []
      } finally {
        this.changeLoading(false)
      }
    }
    // async handleDownload(name) {
    //   try {
    //     this.changeLoading(true)
    //     const date = moment(new Date()).format('YYYY-MM-DD')
    //     await downloadWares({ ...this.handleData(), ...this.pageParams }, `${name} ${date}`)
    //   } finally {
    //     this.changeLoading(false)
    //   }
    // }
  }
}

export const pushFun = {
  methods: {
    async textPushChannel(idList) {
      const { code } = await pushSpu(idList)
      if (code !== '0') return
      this.handleSearch && this.handleSearch()
      this.$message({
        type: 'success',
        message: '已推送！'
      })
    },
    async textPushChannel_edit(idList) {
      // console.log('idList', idList)
      const { code } = await pushSpu_text(idList)
      if (code !== '0') return
      this.handleSearch && this.handleSearch()
      this.$message({
        type: 'success',
        message: '已推送！'
      })
    }
  }
}

export const submit_text = {
  methods: {
    async submit_edit(idList) {
      const { code } = await spuSubmit_text(idList)
      if (code !== '0') return
      this.handleSearch && this.handleSearch()
      this.$message({
        type: 'success',
        message: '提交成功！'
      })
    }
  }
}

export const withdrawFun = {
  methods: {
    async withdrawEdit(idList) {
      const { code, result } = await withdraw_edit(idList)
      if (code !== '0') return
      this.handleSearch && this.handleSearch()
      let message = ''
      if (result && 'failure' in result && 'success' in result && 'total' in result) {
        message = `总共发起了 ${result.total} 条，成功 ${result.success} 条，失败 ${result.failure} 条`
      }
      this.$message({
        type: 'success',
        message: message || '撤回成功！'
      })
    }
  }
}

// spu新增维护作废功能
export const abandonFun = {
  methods: {
    async abandon(idList) {
      const { code, result } = await abandonRequest(idList)
      if (code !== '0') return
      this.handleSearch && this.handleSearch()
      this.$message({
        type: 'success',
        message: result || '作废成功！'
      })
    },
    async abandonEdit(idList) {
      const { code, result } = await abandonEditRequest(idList)
      if (code !== '0') return
      this.handleSearch && this.handleSearch()
      let message = ''
      if (result && 'failure' in result && 'success' in result && 'total' in result) {
        message = `总共发起了 ${result.total} 条，成功 ${result.success} 条，失败 ${result.failure} 条`
      }
      this.$message({
        type: 'success',
        message: message || '作废成功！'
      })
    }
  }
}

// spu新增维护获取主状态
export const getHeaderMainStatus = {
  data() {
    return {
      getStatusListFun: spuListStatus
    }
  },
  created() {
    this.chooseHeaderFun()
    this.getStatus()
  },
  methods: {
    chooseHeaderFun() {
      if (this.menuName === 'newProducts') this.getStatusListFun = spuListStatus
      else if (this.menuName === 'safeguard') this.getStatusListFun = getAllMainStatusTypes
    },
    async getStatus() {
      try {
        const { result } = await this.getStatusListFun()
        if (!result || !Array.isArray(result)) return
        this.options.mainStatus = result.map((_) => {
          return {
            label: _.mainDescribe,
            value: _.mainStatus
          }
        })
        // eslint-disable-next-line no-empty
      } catch (error) {}
    }
  }
}

// spu建品维护操作日志
export const spuOperationLog = {
  data() {
    return {
      title: '操作日志',
      spulogVisible: false,
      spuOperationLogTableHeader,
      operationLogTableList: []
    }
  },
  methods: {
    // 操作日志
    async operationLog({ row }) {
      const flag = this.menuName === 'newProducts'
      const getLogFun = flag ? processData : getProcessLogByChgSpuId
      const key = flag ? 'spuId' : 'chgSpuId'
      const id = row.id
      const { result } = await getLogFun({ [key]: id })
      if (Array.isArray(result) && result.length) {
        this.spulogVisible = true
        this.operationLogTableList = result
      } else {
        this.$message({
          type: 'warning',
          message: '未查询到该商品线上建品的操作日志'
        })
      }
    }
  }
}
